package com.dinian.zeus.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Auther: dinian
 * @Date: 2018/11/19
 */
@Slf4j
public class UploadUtil {

    public static Boolean file(MultipartFile file) throws Exception{

        //String filePath = ResourceUtils.getURL("classpath:").getPath() + Contant.SAVEPATH;   //linux下文件目录
        String filePath = "C:\\file\\";  //windows 文件目录
        String fileName = file.getOriginalFilename();
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        out.write(file.getBytes());
        out.flush();
        out.close();
        return true;
    }

    public static Boolean deleteDir(){
        try {
            File delefile = new File(Contant.WIN_PATH);
            FileUtils.deleteDirectory(delefile);
            log.info("delete directory success ...");
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
